import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 动态路由表：不同的用户访问的功能不同
export const asyncRouter = [
  {
    path: '/departments',
    component: Layout,
    children: [
      {
        path: '', // 作为默认渲染路由
        name: 'departments',
        component: () => import('@/views/departments/departments.vue'),
        meta: { title: '组织架构', icon: 'tree' }
      }
    ]
  },
  {
    path: '/employees',
    component: Layout,
    children: [
      {
        path: '', // 作为默认渲染路由
        name: 'employees',
        component: () => import('@/views/employees/employees.vue'),
        meta: { title: '员工管理', icon: 'people' }
      },
      {
        path: 'detail', // 作为默认渲染路由
        hidden: true,
        component: () => import('@/views/employees/detail')
      }
    ]
  },
  {
    path: '/settings',
    component: Layout,
    children: [
      {
        path: '', // 作为默认渲染路由
        name: 'settings',
        component: () => import('@/views/settings/settings.vue'),
        meta: { title: '公司设置', icon: 'settings' }
      }
    ]
  },
  {
    path: '/permissions',
    component: Layout,
    children: [
      {
        path: '', // 作为默认渲染路由
        name: 'permissions',
        component: () => import('@/views/permissions/permissions.vue'),
        meta: { title: '权限管理', icon: 'lock' }
      }
    ]
  },
  {
    path: '/salarys',
    component: Layout,
    children: [
      {
        path: '', // 作为默认渲染路由
        name: 'salarys',
        component: () => import('@/views/salarys/salarys.vue'),
        meta: { title: '工资管理', icon: 'money' }
      }
    ]
  },
  {
    path: '/social_securitys',
    component: Layout,
    children: [
      {
        path: '', // 作为默认渲染路由
        name: 'social_securitys',
        component: () => import('@/views/social_securitys/social_securitys.vue'),
        meta: { title: '社保管理', icon: 'table' }
      }
    ]
  },
  {
    path: '/attendances',
    component: Layout,
    children: [
      {
        path: '', // 作为默认渲染路由
        name: 'attendances',
        component: () => import('@/views/attendances/attendances.vue'),
        meta: { title: '考勤管理', icon: 'skill' }
      }
    ]
  },
  {
    path: '/approvals',
    component: Layout,
    children: [
      {
        path: '', // 作为默认渲染路由
        name: 'approvals',
        component: () => import('@/views/approvals/approvals.vue'),
        meta: { title: '审批管理', icon: 'tree-table' }
      }
    ]
  }

]

// 静态路由表：所有用户都可以访问的页面
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/import',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'import',
      component: () => import('@/views/employees/import.vue')
    }]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/dashboard'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // routes: [...constantRoutes, ...asyncRouter]
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
